<?php
$acao = "insert";
$titulo = "Novo ";
$codigo = "";

if (isset($_POST["descricao"])) {
    $acao = "update";
    $titulo = "Editar ";
    $codigo = $_POST["codigo"];
}
?>
<div class="mws-panel grid_4 div-center">
    <div class="mws-panel-header">
        <span class="mws-i-24 i-list"><?php echo $titulo ?> tipo de entrada</span>
    </div>
    <div class="mws-panel-body">
        <div id="result"></div>
        <form onsubmit="return false" class="mws-form" method="POST">
            <div class="mws-form-inline">
                <div class="mws-form-row">
                    <label>Descrição</label>
                    <div class="mws-form-item small">
                        <input type="text" name="descricao" id="descricao" class="mws-textinput required"
                        <?php
                        if (isset($_POST["descricao"])) {
                            echo "value='" . $_POST['descricao'] . "'";
                        }
                        ?>
                               />
                    </div>
                </div>
                <div class="mws-form-row">
                    <label>Vincular membro</label>
                    <div class="mws-form-item small">
                        <select class="required meio-campo campo-150 input-small" id="vinculo" name="vinculo">
                            <option value="">Ação</option>
                            <?php
                            foreach ($data as $vinculo) {
                                echo "<option value='" . $vinculo->codigo . "'";

                                if (isset($_POST["vinculo"])) {
                                    if ($_POST["vinculo"] == $vinculo->codigo) {
                                        echo "SELECTED ";
                                    }
                                }

                                echo ">" . $vinculo->vinculo . "</option>";
                            }
                            ?>
                        </select>
                    </div>
                </div>
            </div>
            <div class="mws-button-row">
                <input type="submit" id="salvar-tsaida" value="Salvar" class="mws-button green"/>
                <a href="<?php echo BASE_URL; ?>tipoentrada"><input type="button" value="Cancelar" class="mws-button gray" /></a>
            </div>
        </form>
    </div>    	
</div>

<script type="text/javascript">
            $('#salvar-tsaida').on('click', function() {
                if (__validaForm() && $('#descricao').val().length > 3) {
                    $.ajax({
                        type: 'post',
                        data: 'descricao=' + $('#descricao').val() + "&codigo=<?php echo $codigo; ?>" + "&vinculo=" + $('#vinculo').val(),
                        url: '<?php echo BASE_URL; ?>tipoentrada/<?php echo $acao; ?>',
                        success: function(j) {
                            $("#descricao").val('');
                            var mensg;
                            if (j != "erro") {
                                mensg = "sucesso";
                            } else {
                                mensg = "erro";
                            }

                            $('#result').load("<?php echo BASE_URL; ?>mensagem/" + mensg, function() {
                                location.href = "<?php echo BASE_URL; ?>tipoentrada";
                            });
                        }
                    });
                }
            });
</script>